/*
 * @Author: “ygnnli” “1585923072@qq.com”
 * @Date: 2024-07-30 09:42:31
 * @LastEditors: “ygnnli” “1585923072@qq.com”
 * @LastEditTime: 2024-08-10 17:13:26
 * @FilePath: /110/30/named_pipe/Pipe_Server.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Comm.hpp"
#include <unistd.h>

int main()
{
    Fifo fifo(Path);

    int rfd = open(Path, O_RDONLY); //O_RDONLY 只读打开会自动创建文件

    if (rfd < 0)
    {
        cerr << "open failed, errno: " << errno << ", errstring: " << strerror(errno) << endl;
        return 1;
    }

    // 如果我们的写端没打开，先读打开，open的时候就会阻塞，直到把写端打开，读open才会返回
    cout << "open success" << endl;

    char buffer[1024];
    while (1)
    {
        ssize_t n = read(rfd, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            cout << "client say : " << buffer << endl;
        }
        else if (n == 0)
        {
            cout << "client quit, me too!!" << endl;
            break;
        }
        else
        {
            cerr << "read failed, errno: " << errno << ", errstring: " << strerror(errno) << endl;
            break;
        }
    }

    close(rfd);
    return 0;
}